package service;


import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import dao.UserRepository;
import model.User;

@Service
@Transactional
public class UserService {
	
	@Autowired
	private UserRepository userdao;
	
	/**
	 * 检查用户注册信息
	 * @param username
	 * @param password
	 * @param alias
	 * @param sex
	 * @return
	 */
	public boolean checkUserRegmsg(String username, String password, String alias, Integer sex) {
		
		
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
		{
			return false;
		}
		if(userdao.findByUsername(username) != null) {
			return false;
		}
		
		return true;
		
	}
	
	public void saveUser(User user) {
		userdao.save(user);
	}
	
	
	public User loginUser(String username, String password) {
		
		User user = userdao.findByUsernameAndPassword(username, password);
		
		if(user != null) {
			user.setOnlineTime(new Date());
		}
		
		return user;
	}
	
	
	
}
